<template>
  <div>
    <el-row :gutter="10">
      <el-col :span="2.5">
        可提现金额 (客户确认收货，或者超过退换货时间才会计入可提现金额中)
      </el-col>
      <el-col :span="3">
        <el-input v-model="tixje" :clearable="true" size="mini" />
      </el-col>
      <el-col :span="2">
        <el-button type="primary" size="mini" @click="tixian()">提现</el-button>
      </el-col>
    </el-row>

    <hr />

    <el-table :data="tableData">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column label="名称" width="300">
        <template slot-scope="scope">{{ scope.row.summary }}</template>
      </el-table-column>
      <el-table-column label="类型">
        <template slot-scope="scope">{{
          scope.row.type | formatType
        }}</template>
      </el-table-column>
      <el-table-column label="金额(元)" width="120">
        <template slot-scope="scope">{{ scope.row.money }}</template>
      </el-table-column>
      <el-table-column label="日期">
        <template slot-scope="scope">{{
          scope.row.date | formatDate
        }}</template>
      </el-table-column>
    </el-table>
    <br />
    <el-pagination background layout="prev, pager, next" :total="total" @current-change="currentChange" :page-size="10"></el-pagination>
  </div>
</template>

<script>
import * as shopBase from "@/api/shop.base";
export default {
  props: ["inputParam"],
  data() {
    return {
      tixje: null,
      tableData: [],
      listQuery: {
        pageStart: 1,
        pageSize: 10,
      },
      total: 0
    };
  },
  mounted() {
    this.getList();
    this.tixje = this.inputParam.withdrawable;
  },
  filters: {
    formatType(val) {
      if (val == 1) return "收入";
      if (val == 2) return "支出";
    },

    formatDate(val) {
      var time = new Date(val);

      function timeAdd0(str) {
        if (str < 10) {
          str = "0" + str;
        }
        return str;
      }
      var y = time.getFullYear();
      var m = time.getMonth() + 1;
      var d = time.getDate();
      var h = time.getHours();
      var mm = time.getMinutes();
      var s = time.getSeconds();
      return (
        y +
        "-" +
        timeAdd0(m) +
        "-" +
        timeAdd0(d) +
        " " +
        timeAdd0(h) +
        ":" +
        timeAdd0(mm) +
        ":" +
        timeAdd0(s)
      );
    }
  },
  methods: {
    currentChange(current) {
      this.listQuery.pageStart = current;
      this.getList();
    },

    tixian() {
      let _money = this.tixje;
      if (_money > this.inputParam.withdrawable) {
        this.$message({
          message: "提现金额不能大于账户余额",
          type: "error"
        });

        return;
      }

      shopBase.withdrawable({ money: _money }).then(resp => {
        console.log(resp);
        if (resp.success) {
          this.$message({
            message: "提现成功",
            type: "success"
          });

          this.getList();
        } else {
          this.$message({
            message: resp.bizMessage,
            type: "error"
          });
        }
      });
    },

    getList() {
      let that = this;

      shopBase.billList(this.listQuery).then(resp => {
        if (resp.success) {
          that.tableData = resp.data.list;
          that.total = resp.data.totalCount
        } 
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
